package cn;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.*;

public class Test85 {
    Document document = null;

    public static void main(String[] args) {
        Test85 test85 = new Test85();
        test85.init();
        //test85.add();
        //test85.update();
        test85.del();
    }

    //初始化，读取xml文件
    public void init() {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        try {
            DocumentBuilder builder = factory.newDocumentBuilder();
            document = builder.parse("src/cn/clothes.xml");
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void show() {
        Element element = document.getDocumentElement();
        System.out.println(element.getTagName());
        NodeList nodeList = element.getElementsByTagName("type");
        System.out.println(nodeList.getLength());
        for (int i = 0; i < nodeList.getLength(); i++) {
            Node node = nodeList.item(i);
            Element ele = (Element) node;
            System.out.println(ele.getTextContent());
        }
    }

    public void del() {
        Element element = document.getDocumentElement();
        NodeList nodeList = element.getElementsByTagName("type");
        for (int i = 0; i < nodeList.getLength(); i++) {
            Node node = nodeList.item(i);
            Element ele = (Element) node;
            if(ele.getAttribute("xh").equals("s")){
                ele.getParentNode().removeChild(ele);
            }
        }
        save("newxml2.xml");
    }

    /**
     * 修改
     */
    public void update() {
        Element element = document.getDocumentElement();
        NodeList nodeList = element.getElementsByTagName("type");
        for (int i = 0; i < nodeList.getLength(); i++) {
            Node node = nodeList.item(i);
            Element ele = (Element) node;
            ele.setAttribute("id", (i + 1) + "");
        }
        save("newxml.xml");
    }


    public void add() {
        //创建标签元素  节点
        Element type = document.createElement("type");
        //给节点设置属性
        type.setAttribute("xh", "xxl");
        //给节点设置文本节点
        type.setTextContent("195<身高<199");
        //将新的节点添加到根节点
        document.getElementsByTagName("yifu").item(0).appendChild(type);
        //将内存里的修改后的xml文件保存到一个新的文件
        save("newyifu2.xml");
    }

    public void save(String name) {
        //创建转储工厂
        TransformerFactory factory = TransformerFactory.newInstance();
        //设置缩进
        factory.setAttribute("indent-number", "4");
        try {
            //获得转储对象
            Transformer transformer = factory.newTransformer();
            //OutputKeys.ENCODING
            //设置编码
            transformer.setOutputProperty("encoding", "utf-8");
            //启用缩进
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
            //创建新的存储路径文件
            StreamResult result = new StreamResult(new OutputStreamWriter(new FileOutputStream(name), "utf-8"));
            //获得xml文档
            DOMSource source = new DOMSource(document);
            //存储
            transformer.transform(source, result);
        } catch (TransformerConfigurationException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (TransformerException e) {
            e.printStackTrace();
        }
    }
}
